library pp_page;

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pp_page/pp_page_controller.dart';
import 'package:pp_page/pp_page_permission_mixin.dart';
import 'package:pp_uikit/pp_appbar.dart';
import 'package:pp_uikit/theme/pp_appbar_theme.dart';

export 'package:flutter/material.dart';
export 'package:get/get.dart';
export 'package:pp_page/pp_page_controller.dart';
export 'package:pp_uikit/pp_appbar.dart';
export 'package:pp_uikit/pp_uikit.dart';

// ignore: must_be_immutable
abstract class PPPage<T extends PPPageController> extends GetView<T>
    with PPPagePermissionMixin {
  PPPage({Key? key}) : super(key: key);

  @override
  // ignore: overridden_fields
  final String tag = "${DateTime.now()}";
  T bindController(BuildContext context);

  String? bindControllerTag() {
    return null;
  }

  @override
  T get controller {
    var cusTag = bindControllerTag();
    return GetInstance().find<T>(tag: cusTag ?? tag);
  }

  @override
  Widget build(BuildContext context) {
    var cusTag = bindControllerTag();
    Get.put<T>(bindController(context), tag: cusTag ?? tag);

    return Scaffold(
      backgroundColor: backgroundColor(),
      key: controller.scaffoldKey,
      appBar: buildAppBar(context) ?? _buildAppBarDynamic(context),
      floatingActionButton: buildFloatingActionButton(context),
      floatingActionButtonLocation: floatingActionButtonLocation(context),
      body: GestureDetector(
        child: Container(
          color: safeAreaBackgroundColor(),
          child: buildBodyWithoutSafeArea(context) ??
              SafeArea(
                top: false,
                child: Stack(
                  children: [
                    Container(
                      color: backgroundColor(),
                    ),
                    buildBody(context),
                  ],
                ),
              ),
        ),
        onTap: () {
          if (isTapPageHideKeyboard()) {
            controller.hideKeyboard(context);
          }
        },
        onPanDown: (details) {
          if (isTapPageHideKeyboard()) {
            controller.hideKeyboard(context);
          }
        },
      ),
      bottomNavigationBar: buildBottomNavigationBar(),
      endDrawer: buildEndDrawer(),
    );
  }

  Color backgroundColor() {
    return const Color(0xFFF7F7F7);
  }

  Color safeAreaBackgroundColor() {
    return const Color(0xFFF7F7F7);
  }

  /// 点击页面是否收起键盘
  bool isTapPageHideKeyboard() {
    return true;
  }

  PPAppBar? buildAppBar(BuildContext context) {
    return null;
  }

  PreferredSize? _buildAppBarDynamic(BuildContext context) {
    var appBar = buildAppBarDynamic(context);
    if (appBar == null) {
      return null;
    }
    return PreferredSize(
      preferredSize: const Size.fromHeight(PPAppBarTheme.appBarHeight),
      child: appBar,
    );
  }

  /// 悬浮按钮
  Widget? buildFloatingActionButton(BuildContext context) {
    return null;
  }

  /// 自定义悬浮按钮位置
  FloatingActionButtonLocation? floatingActionButtonLocation(
      BuildContext context) {
    return CustomFloatingActionButtonLocation(
        FloatingActionButtonLocation.endFloat, 0, -50);
  }

  /// appbar 需要动态时，使用这个
  Obx? buildAppBarDynamic(BuildContext context) {
    return null;
  }

  Widget buildBody(BuildContext context);

  Widget? buildBodyWithoutSafeArea(BuildContext context) {
    return null;
  }

  Widget? buildBottomNavigationBar() {
    return null;
  }

  Widget? buildEndDrawer() {
    return null;
  }
}

class CustomFloatingActionButtonLocation extends FloatingActionButtonLocation {
  FloatingActionButtonLocation location;
  double offsetX; // X方向的偏移量
  double offsetY; // Y方向的偏移量
  CustomFloatingActionButtonLocation(this.location, this.offsetX, this.offsetY);

  @override
  Offset getOffset(ScaffoldPrelayoutGeometry scaffoldGeometry) {
    Offset offset = location.getOffset(scaffoldGeometry);
    return Offset(offset.dx + offsetX, offset.dy + offsetY);
  }
}
